/************************************************************************************
* PlaylistModel.h                                                                   *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                                *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/

#ifndef PlaylistModel_H
#define PlaylistModel_H

#include <QList>
#include <QSet>
#include <QAbstractTableModel>

#include "common/MediaItem.h"
#include "interfaces/Playlist.h"

class PlaylistModel : public QAbstractTableModel, public Yamp::Playlist
{
    //    qint32 m_prevIndex;
    Q_OBJECT

    QList<MediaItem*>   m_sources;
    QList<qint32>       m_playedIndexList;
    qint32              m_prevListIndex;
    qint32              m_currentIndex;
    bool                m_randomize;
    LoopType            m_loopType;

    QSet<QString>       m_randTracks;
    QList<IPlaylistListener*>    m_listeners;
    void setNextIndex();

signals:
    void selected(MediaItem* source, int index);
    void playlistChanged();
    void clearingPlaylist();
    void removingCurrentItem();
    void randomConfigChanged(bool value);
    void repeatConfigChanged(LoopType newType, LoopType oldType);

private slots:
    void notifySelected(MediaItem* source);
    void notifyPlaylistChanged();
    void addListener(IPlaylistListener *listner);
    void cleanTempStructs(bool rndInfoAlso = false);
    void appendPlayedIndex(qint32 index);
//    void adjustCacheMaxCost();

public slots:
    void selectNext();
    void selectPrev();
    void selectIndex(qint32 index);
    void selectIndex(QModelIndex index);
    void append(MediaItem* item, bool play = true);
    void append(QList<MediaItem*>* items, bool play = true);
    void insertAsCurrent(MediaItem* item, bool play = true);
    void insert(int index, MediaItem* item, bool play = true);
    void removeItem(qint32 index);
    void removeItems(QList<int>& indices);
    void setRandom(bool random);
    void setRepeat (LoopType loopType);
    void moveUp(int index);
    void moveDown(int index);
    void clear();
    void clear(bool notify);
    void itemRemoved(QList<QString> *ids);


public:
    PlaylistModel(QObject* parent);
    bool hasNext();
    bool hasPrev();
    bool isRandom();
    bool isValid();
    LoopType repeat();
    int currentIndex();
    MediaItem* currentTrack();
    quint32 numItems();
    virtual ~PlaylistModel();

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    QModelIndex index(int row, int column, const QModelIndex &parent) const;
    QModelIndex parent(const QModelIndex &child) const;
    bool hasChildren(const QModelIndex &parent) const;
};

#endif // PlaylistModel_H
